﻿using OAS.Application.DepartmentApp.Services;
using OAS.Application.System.Dtos;
using OAS.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace OAS.Application.DepartmentApp.Services
{
    public class DepartmentService : Repository<Department>, IDepartmentService, ITransient
    {
        public async Task<List<Department>> GetAllDepartment()
        {
            Expression<Func<Department, bool>> sqlWhere = Expressionable.Create<Department>()
              .AndIF(UserContext.CurrentUser.RoleName != "总经理" && !UserContext.IsAdmin, o => o.Department_Id == UserContext.CurrentUser.Department_Id)
              .ToExpression();
            return await base.GetListAsync(sqlWhere);
        }
        public async Task<object> GetDepartmentList(DtoQuery dto)
        {
            RefAsync<int> total = 0;
            List<Department> list = await base.AsQueryable().Where(o => o.DepartmentName.Contains(dto.KeyWord ?? "")).ToPageListAsync(dto.PageIndex, dto.PageSize, total);
            return new { list, total };
        }
        public async Task AddDepartment(Department department)
        {
            if (base.AsQueryable().Any(o => o.DepartmentName == department.DepartmentName)) throw Oops.Oh("该部门已存在");
            await base.InsertAsync(department);
        }
        public async Task UpdateDepartment(Department department)
        {
            if (base.AsQueryable().Any(o => o.DepartmentName == department.DepartmentName)) throw Oops.Oh("该部门已存在");
            await base.UpdateAsync(department);
        }
        public async Task DeleteDepartment(string[] ids)
        {
            await base.DeleteByIdsAsync(ids);
        }
    }
}
